package ui

import (
	"fmt"
	"math"
	"strconv"
)

func prettyPercent(f float64) string {
	return prettyPercentFixed(f, 2)
}

func prettyPercentFixed(f float64, decimalNumber uint) string {
	ff := roundFloat(f, decimalNumber)
	return strconv.FormatFloat(ff, 'f', -1, 64) + "%"
}

func roundFloat(val float64, precision uint) float64 {
	ratio := math.Pow(10, float64(precision))
	return math.Round(val*ratio) / ratio
}

func PercentUpColorCSSClass(f float64) string {
	return PercentColorCSSClassCustom(f, 90.0, false)
}

func PercentDownColorCSSClass(f float64) string {
	return PercentColorCSSClassCustom(f, 10.0, true)
}

// PercentColorCSSClassCustom returns a CSS class based on the given percent value (f).
// If inverse is true, lower values are considered worse.
// warningThreshold defines the threshold for warning state.
func PercentColorCSSClassCustom(f float64, warningThreshold float64, inverse bool) string {
	const (
		cssClassOK      = "is-ok"
		cssClassWarning = "is-warning"
		cssClassDanger  = "is-critical"
	)
	if inverse {
		if f < 0 {
			return cssClassDanger
		} else if f < warningThreshold {
			return cssClassWarning
		}

	} else {
		if f > 100 {
			return cssClassDanger
		} else if f >= warningThreshold {
			return cssClassWarning
		}
	}

	return cssClassOK
}

func SlothLogoSVG(color string, width int) string {
	const svgFmt = `
<svg width="%d" viewBox="0 0 1525 1858" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M1525 1095.38C1525 1516.5 1183.62 1857.88 762.5 1857.88C341.383 1857.88 0 1516.5 0 1095.38C0 674.266 341.383 792.757 762.5 792.757C1183.62 792.757 1525 674.266 1525 1095.38Z" fill="none"/>
<path d="M0.41515 1126.65C0.41515 1132.56 1.00754 1138.48 1.36298 1144.4L1.71842 1150.32C2.84396 1166.24 4.50267 1182.04 6.57605 1197.66C6.57605 1199.02 6.99072 1200.44 7.16844 1201.8C8.11627 1208.72 9.18258 1215.59 10.3081 1222.45L10.8413 1225.59C13.6255 1241.74 16.7652 1257.66 20.675 1273.46L21.0305 1275C22.8669 1282.58 24.8218 1290.15 26.9544 1297.61C31.1604 1095.4 147.27 922.49 313.673 841.241L325.995 835.323C339.522 829.916 353.959 827.144 368.529 827.157C384.578 827.154 400.451 830.5 415.131 836.981C429.811 843.462 442.973 852.935 453.774 864.793C473.313 885.96 484.856 913.268 486.415 942.018C486.415 944.503 486.415 946.988 486.415 949.533C486.422 959.495 485.269 969.425 482.979 979.121C480.384 990.947 476.074 1002.33 470.184 1012.91C465.595 1020.89 460.109 1028.31 453.833 1035.04C441.589 1048.1 426.541 1058.22 409.819 1064.63C216.639 1158.66 157.636 1325 151.713 1433.53C151.489 1485.27 160.434 1536.64 178.133 1585.26L179.674 1587.04C191.837 1601.4 204.495 1615.32 217.646 1628.81L220.312 1631.54C226.71 1638.05 233.265 1644.44 239.979 1650.71L242.763 1653.31C256.507 1666.13 270.744 1678.44 285.475 1690.24C286.423 1691.07 287.43 1691.84 288.437 1692.61C295.605 1698.52 302.832 1703.91 310.237 1709.29L313.436 1711.66C328.601 1722.75 344.201 1733.28 360.235 1743.26L363.612 1745.33C371.372 1750.12 379.251 1754.74 387.308 1759.3L390.921 1761.31C407.39 1770.46 424.214 1779.06 441.393 1787.11L445.066 1788.83C453.438 1792.77 461.87 1796.44 470.361 1799.83L474.449 1801.49C491.984 1808.67 509.755 1815.2 527.764 1821.08L531.792 1822.38C540.619 1825.16 549.564 1827.76 558.45 1830.25L562.893 1831.43C572.134 1833.98 581.435 1836.34 590.854 1838.47C689.723 1861.13 792.106 1863.98 892.086 1846.88L899.965 1845.46L914.479 1842.68L923.957 1840.66C928.696 1839.66 933.376 1838.59 938.115 1837.47L946.054 1835.63C952.688 1833.98 959.323 1832.2 965.899 1830.43L974.844 1827.82L987.877 1823.98L996.703 1821.19C1003.22 1819.12 1009.68 1816.93 1016.07 1814.69L1023.54 1811.96L1037.05 1806.93L1045.75 1803.5L1059.32 1797.94L1066.31 1794.98C1072.51 1792.26 1078.67 1789.48 1084.79 1786.64L1092.91 1782.73C1097.05 1780.78 1101.14 1778.71 1105.23 1776.81L1113.05 1772.85C1119.09 1769.73 1125.02 1766.54 1130.82 1763.26L1137.46 1759.47C1141.66 1757.11 1145.87 1754.68 1150.01 1752.2C1152.62 1750.72 1155.17 1749.18 1157.72 1747.64C1162.1 1744.98 1166.42 1742.25 1170.75 1739.53L1176.67 1735.86C1182.3 1732.19 1187.87 1728.41 1193.44 1724.62L1200.43 1719.77L1211.98 1711.48L1218.55 1706.63C1224 1702.55 1229.45 1698.4 1234.79 1694.2L1240.35 1689.71L1252.2 1680.18L1258.66 1674.73C1262.81 1671.18 1266.89 1667.57 1270.98 1663.96L1275.42 1659.94C1280.46 1655.38 1285.38 1650.77 1290.23 1646.09L1296.16 1640.53C1299.71 1637.04 1303.27 1633.55 1306.7 1630C1308.48 1628.22 1310.26 1626.45 1311.98 1624.61C1316.77 1619.64 1321.51 1614.61 1326.19 1609.46C1327.73 1607.87 1329.15 1606.15 1330.64 1604.49C1334.13 1600.65 1337.57 1596.74 1341 1592.66L1346.1 1586.74C1349.95 1582.24 1353.68 1577.69 1357.35 1573.13L1360.49 1569.22C1364.05 1564.79 1367.54 1560.23 1370.98 1555.67C1381.44 1515.92 1386.67 1474.99 1386.56 1433.89C1382.77 1372.58 1326.61 1185.7 1128.45 1064.99C1102.98 1055.06 1081.67 1036.75 1068.03 1013.09C1061.99 1002.69 1057.5 991.463 1054.7 979.772C1052.39 970.078 1051.22 960.149 1051.2 950.184C1051.2 947.639 1051.2 945.154 1051.2 942.669C1051.79 932.483 1053.62 922.408 1056.65 912.666C1062.1 895.057 1071.41 878.883 1083.9 865.326C1094.7 853.468 1107.87 843.995 1122.55 837.513C1137.23 831.032 1153.1 827.686 1169.15 827.69C1183.72 827.683 1198.15 830.454 1211.68 835.856L1223.95 841.774C1378.5 917.223 1489.63 1072.56 1508.29 1256.01C1513.47 1231.74 1517.42 1207.09 1520.14 1182.04L1520.85 1176.12C1521.33 1171.8 1521.68 1167.42 1522.04 1163.1L1522.93 1152.68L1523.64 1141.68C1523.93 1137.12 1524.11 1132.51 1524.29 1127.95C1524.29 1125.11 1524.29 1122.03 1524.64 1119.37C1524.64 1111.79 1525 1104.16 1525 1096.53C1525.09 962.41 1489.72 830.65 1422.45 714.579C1355.19 598.508 1258.43 502.245 1141.96 435.531C1140.18 434.584 1138.46 433.578 1136.75 432.572C1062.73 389.979 1003.58 325.708 967.319 248.449C931.053 171.19 919.411 84.6701 933.969 0.587524C759.164 35.0608 601.431 128.248 486.983 264.664C372.534 401.08 308.262 572.51 304.846 750.465C254.579 697.577 226.602 627.402 226.71 554.474C154.804 625.229 97.7246 709.579 58.8006 802.604C19.8766 895.629 -0.11215 995.465 0.000473277 1096.29C0.000473277 1104.16 0.000474292 1112.09 0.41515 1119.96C0.41515 1122.15 0.41515 1124.4 0.41515 1126.65ZM768.869 948.349C845.524 948.349 907.666 984.802 907.666 1029.72C907.666 1074.63 845.524 1111.08 768.869 1111.08C692.213 1111.08 630.071 1074.69 630.071 1029.72C630.071 984.743 692.45 948.349 769.105 948.349H768.869Z" fill="%s"/>
</svg>
`
	if color == "" {
		color = "currentcolor"
	}

	if width <= 0 {
		width = 1525
	}

	return fmt.Sprintf(svgFmt, width, color)
}

const (
	iconSortAsc   = `↑`
	iconSortDesc  = `↓`
	iconSortUnset = `⇅`
)
